package com.csdj.x315springboot.util;


import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {
    private static final char[] HEX_CHARS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
    private static final Charset UTF8_CHARSET = Charset.forName("UTF8");
    public static String sha256(String msg){
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            digest.update(msg.getBytes());
            byte[] res = digest.digest();
            return new String(encodeHex(res));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }


    private static char[] encodeHex(byte[] data) {
        int l = data.length;
        char[] out = new char[l << 1];
        int i = 0;

        for(int var4 = 0; i < l; ++i) {
            out[var4++] = HEX_CHARS[(240 & data[i]) >>> 4];
            out[var4++] = HEX_CHARS[15 & data[i]];
        }

        return out;
    }


    public static void main(String[] args) {
        System.out.println(sha256("123456"));
    }

}
